﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NHibernate;
using Inovout.Repositories;
using Inovout.Cloud.DataStore.Models;

namespace Inovout.Cloud.DataStore.Repositories
{
    public class TableRepository : DataObjectRepository<Table>
    {
        public override void Delete(Table table)
        {
            using (ISession session = OpenSession())
            {
                //String hql = "from Property as c where c.Table.id=" + (object)table.Id;
                //IList<Property> propertyList = session.CreateQuery(hql).List<Property>();
                foreach (Property property in table.Properties)
                {
                    session.Delete(property);
                }

                //hql = "from Grab as c where c.Table.id=" + (object)table.Id;
                //IList<Grab> grabList = session.CreateQuery(hql).List<Grab>();
                foreach (Grab grab in table.Grabs)
                {
                    table.Grabs.Remove(grab);
                    session.Delete(grab);
                }

                table.Database.Tables.Remove(table);
                table.Database = null;
                session.Delete(table);
                session.Flush();
            }
        }
    }
}
